using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClimbTracker.BusinessLogic.Entities;

namespace ClimbTracker.GUI.EntityForms
{
    public partial class CompetitorAddForm : Form
    {

        private Competitor _competitor = new Competitor();
        private bool newComeptitorAdded = false;

        public Competitor Competitor
        {
            get { return _competitor; }
            set { _competitor = value; }
        }
        public bool NewComeptitorAdded
        {
            get { return newComeptitorAdded; }
            set { newComeptitorAdded = value; }
        }




        public CompetitorAddForm()
        {
            InitializeComponent();
        }



        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {

            if (!isMissingFields())
            {
                // Assign the controls' values back to the competitor object
                Competitor.CompetitorID = 0;
                Competitor.Name = NameTextBox.Text;
                Competitor.Age = int.Parse(AgeNumericUpDown.Value.ToString());
                Competitor.Nationality = NationalityTextBox.Text;
                Competitor.Category = CategoryTextBox.Text;
                Competitor.Ranking = int.Parse(rankingNumericUpDown.Value.ToString());
                Competitor.PicturePath = picturePathLabel.Text;

                NewComeptitorAdded = true;

                this.Close();
            }
        }



        private bool isMissingFields()
        {
            // Accumulate the missing fields
            String emptyFields = "";
            if (NameTextBox.Text.Trim().Length == 0)
            {
                emptyFields += "Name, ";
            }
            if (AgeNumericUpDown.Value == 0)
            {
                emptyFields += "Age, ";
            }
            if (NationalityTextBox.Text.Trim().Length == 0)
            {
                emptyFields += "Nationality, ";
            }
            if (CategoryTextBox.Text.Trim().Length == 0)
            {
                emptyFields += "Category, ";
            }
            if (rankingNumericUpDown.Value == 0)
            {
                emptyFields += "Ranking, ";
            }

            if (emptyFields.Length > 0)
            {
                emptyFields.Trim();
                emptyFields = emptyFields.Substring(0, emptyFields.Length - 2);

                MessageBox.Show("You must fill in the following fields:\n" + emptyFields, "Missing Fields",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }


            return false;
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
            
            // Update the pictureBox and picture path only if the user selected a picture
            if (openFileDialog.FileName != null && openFileDialog.FileName.Length > 0)
            {
                Competitor.PicturePath = openFileDialog.FileName;
                pictureBox.Load(Competitor.PicturePath);
                picturePathLabel.Text = Competitor.PicturePath;
            }

        }

        private void CompetitorAddForm_Load(object sender, EventArgs e)
        {
            competitorIDLabel.Text = "Will be determined by database";
        }
    }
}